﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;

namespace ExpertMIS.Service
{
    public partial class GetPhoto : System.Web.UI.Page
    {
        private void RetrieveUserPhoto()
        {
            string filepath = Server.MapPath(ConfigurationManager.AppSettings["UserUpload"]);

            // Modify, wdong, 2009-09-02,add default nopic.gif
            string filename = Request["filename"];
            if (string.IsNullOrEmpty(filename) || filename.Trim().Length == 0)
            {
                filename = "nopic.gif";
            }

            if (System.IO.File.Exists(filepath + filename))
            {
                try
                {
                    int w = 0;
                    int h = 0;
                    if (!int.TryParse(Request["w"], out w))
                    {
                        w = 0;
                    }
                    if (!int.TryParse(Request["h"], out h))
                    {
                        h = 0;
                    }
                    if (w == 0 || h == 0)
                    {
                        System.Drawing.Image img = System.Drawing.Image.FromFile(filepath + filename);
                        Response.Clear();
                        img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                        img.Dispose();
                        Response.End();
                    }
                    else
                    {
                        string newName = System.IO.Path.GetFileNameWithoutExtension(filename) + "_zoom" + w.ToString() + System.IO.Path.GetExtension(filename);
                        if (System.IO.File.Exists(filepath + newName))
                        {
                            System.Drawing.Image img = System.Drawing.Image.FromFile(filepath + filename);
                            Response.Clear();
                            img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                            img.Dispose();
                            Response.End();
                        }
                        else
                        {
                            ZoomPic(filepath + filename, filepath + newName, (w > h ? w : h));
                        }
                    }
                }
                catch
                {
                    // TODO:Diddy@2009-09-06, write log here
                }
            }
            else if (System.IO.File.Exists(filepath + "nopic.gif"))// 没有图片显示
            {
                System.Drawing.Image img = System.Drawing.Image.FromFile(filepath + "nopic.gif");
                Response.Clear();
                img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                img.Dispose();
                Response.End();
            }
        }

        public void ZoomPic(string originalFilename, string outputFilename, int MaxLength)
        {
            if (!System.IO.File.Exists(originalFilename))
            {
                return;
            }
            Bitmap bmpOld = new Bitmap(originalFilename);

            //计算缩小比例
            double d1;
            if (bmpOld.Height > bmpOld.Width)
                d1 = (double)(MaxLength / (double)bmpOld.Width);
            else
                d1 = (double)(MaxLength / (double)bmpOld.Height);

            //产生缩图
            Bitmap bmpThumb = new Bitmap(bmpOld, (int)(bmpOld.Width * d1), (int)(bmpOld.Height * d1));

            // 清除缓冲 
            Response.Clear();
            //生成图片
            bmpThumb.Save(Response.OutputStream, ImageFormat.Jpeg);
            bmpThumb.Save(outputFilename);
            Response.End();
            //释放资源
            bmpThumb.Dispose();
            bmpOld.Dispose();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RetrieveUserPhoto();
        }
    }
}
